<?php

namespace App\Models\ReturnProduct;

use App\Models\Customers\Customer;
use App\Models\Order\Order;
use App\Models\Order\OrderProduct;
use App\Services\Locale\Language;
use App\Services\MobileApp\AppEnv;
use App\Services\Utils\ApiChecker;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class ReturnProduct
 * @property string product_id
 * @property Collection orderProducts
 * @property Order order
 * @property string quantity
 * @property ReturnReason returnReason
 * @property string return_id
 * @property Collection returnHistories
 * @property string quantiry
 * @property string comment
 * @property string fullname
 * @property string telephone
 * @package App\Models\ReturnProduct
 *
 * @method static self filterOrderProductId($order_product_id)
 *
 * @mixin Builder
 */

class ReturnProduct extends Model
{
    use ApiChecker;

    protected $table = 'return';
    protected $guarded = [];
    protected $primaryKey = 'return_id';

    public $timestamps = false;

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'order_id');
    }

    /**
     * 本来是belongTo关系的，为了方便查询改成hasMany。可以和订单详情接口共用商品详情的数据
     *
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
     */
    public function orderProducts()
    {
        return $this->hasMany(OrderProduct::class, 'order_product_id', 'order_product_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function returnAction()
    {
        return $this->belongsTo(ReturnAction::class, 'return_action_id', 'return_action_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function returnReason()
    {
        return $this->belongsTo(ReturnReason::class, 'return_reason_id', 'return_reason_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function returnStatus()
    {
        return $this->belongsTo(ReturnStatus::class, 'return_status_id', 'return_status_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function returnHistories()
    {
        return $this->hasMany(ReturnHistory::class, 'return_id', 'return_id')
            ->latest('date_added');
    }

    /**
     * @used-by filterOrderProductId()
     *
     * @param Builder $query
     * @param $order_product_id
     * @return Builder
     */
    public function scopeFilterOrderProductId(Builder $query, $order_product_id)
    {
        if (!$order_product_id) return $query;

        return $query->where('order_product_id', $order_product_id);
    }

    /**
     * @param $order_product_id
     * @return ReturnProduct|Model|null|object
     */
    public static function getReturnProduct($order_product_id)
    {
        $return_product = (new self)->where('order_product_id', $order_product_id)
            ->first();

        return $return_product;
    }

    /**
     * @param \Illuminate\Support\Collection $order_products
     * @return bool
     */
    public static function hasReturnProduct(\Illuminate\Support\Collection $order_products)
    {
        $order_products = $order_products->filter(function(OrderProduct $order_product){
            return self::getReturnProduct($order_product->order_product_id);
        });

        return (bool)$order_products->first();
    }
}
